/**
 * 
 */
package com.ph.shopping.facade.member.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.ph.shopping.common.util.container.ParamVerifyUtil;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.facade.duboresult.DubboResult;
import com.ph.shopping.facade.member.dto.MemberInfoDto;
import com.ph.shopping.facade.member.dto.MemberLoginDto;
import com.ph.shopping.facade.member.dto.MemberPageDto;
import com.ph.shopping.facade.member.dto.MemberPasswordDto;
import com.ph.shopping.facade.member.dto.PayPasswordDto;
import com.ph.shopping.facade.member.dto.RegisterMemberDto;
import com.ph.shopping.facade.member.entity.Member;
import com.ph.shopping.facade.member.entity.TradersPassword;
import com.ph.shopping.facade.member.service.IMemberService;
import com.ph.shopping.facade.member.service.dbservice.facade.IMemberDbService;
import com.ph.shopping.facade.member.vo.MemberPhoneUpdataVo;

/**
 * 
 * @ClassName: MemberService
 * @Description:会员操作服务类
 * @author tony
 * @date 2017年3月14日
 *
 */
@Component
@Service(version = "1.0.0")
public class MemberService implements IMemberService {

	private static final Logger log = LoggerFactory.getLogger(MemberService.class);

	@Autowired
	private IMemberDbService memberDbService;
	
	@Override
	public Result getMemberListByPage(MemberPageDto pageInfo) {
		log.info("根据分页信息获取会员列表参数,MemberPageDto ={} ", JSON.toJSONString(pageInfo));
		Result result = DubboResult.getDefResult();
		try {
			String[] fields = { "pageNum", "pageSize" };
			if (ParamVerifyUtil.entityIsNotNullByField(pageInfo, fields)) {
				result = memberDbService.queryMemberListByPage(pageInfo);
			}
		} catch (Exception e) {
			result = DubboResult.getResultByError(result);
			log.error("根据分页参数查询会员信息错误", e);
		}
		return result;
	}

	@Override
	public Result getMemberList(MemberPageDto dto) {
		log.info("获取会员列表参数,MemberPageDto = {} ", JSON.toJSONString(dto));
		try {
			return memberDbService.queryMemberList(dto);
		} catch (Exception e) {
			log.error("查询会员列表数据错误", e);
			return DubboResult.getResultByError(null);
		}
	}

	@Override
	public Result addMember(RegisterMemberDto dto) {
		log.info("添加会员参数,RegisterMemberDto = {} ", JSON.toJSONString(dto));
		Result result = DubboResult.getDefResult();
		try {
			if (dto != null) {
				Member member = dto.getMeber();
				String[] fields = { "memberPhone", "memberName" };
				if (ParamVerifyUtil.entityIsNotNullByField(member, fields)) {
					result = memberDbService.addMember(dto);
				}
			}
		} catch (Exception e) {
			log.error("添加会员信息错误", e);
			result = DubboResult.getResultByError(result);
		}
		return result;
	}

	@Override
	public Result updateMember(MemberInfoDto memberUpdateDto) {
		log.info("修改会员参数,MemberInfoDto = {} ", JSON.toJSONString(memberUpdateDto));
		Result result = DubboResult.getDefResult();
		try {
			String[] fields = { "id" };
			if (ParamVerifyUtil.entityIsNotNullByField(memberUpdateDto, fields)) {
				result = memberDbService.updateMember(memberUpdateDto);
			}
		} catch (Exception e) {
			log.error("修改会员数据错误", e);
			result = DubboResult.getResultByError(result);
		}
		return result;
	}

	@Override
	public Result deleteMember(Integer id) {
		log.info("根据会员ID 删除会员参数,id  " + id);
		Result result = DubboResult.getDefResult();
		try {
			if (id != null) {
				result = memberDbService.deleteMember(id);
			}
		} catch (Exception e) {
			log.error("删除会员数据错误", e);
			result = DubboResult.getResultByError(result);
		}
		return result;
	}

	@Override
	public Result batchUpdateMembers(List<MemberInfoDto> list) {
		log.info("批量修改会员参数,List<MemberInfoDto> = {} ", JSON.toJSONString(list));
		Result result = DubboResult.getDefResult();
		try {
			if (ParamVerifyUtil.objIsNotNull(list)) {
				result = memberDbService.batchUpdateMembers(list);
			}
		} catch (Exception e) {
			log.error("批量修改数据错误");
			result = DubboResult.getResultByError(result);
		}
		return result;
	}

	@Override
	public Result batchDeleteMembers(List<Integer> list) {
		log.info("批量删除会员参数,List<Integer> = {} ", JSON.toJSONString(list));
		Result result = DubboResult.getDefResult();
		try {
			if (ParamVerifyUtil.objIsNotNull(list)) {
				result = memberDbService.batchDeleteMembers(list);
			}
		} catch (Exception e) {
			log.error("批量删除数据错误");
			result = DubboResult.getResultByError(result);
		}
		return result;
	}

	@Override
	public Result getMemberInfoByMobile(String mobile) {
		log.info("根据手机号获取会员数据会员参数,mobile : " + mobile);
		Result result = DubboResult.getDefResult();
		try {
			if (StringUtils.isNotBlank(mobile)) {
				result = memberDbService.queryMemberInfoByMobile(mobile);
			}
		} catch (Exception e) {
			log.error("根据手机号查询会员信息错误");
			result = DubboResult.getResultByError(result);
		}
		return result;
	}

	@Override
	public Result registerMember(RegisterMemberDto dto) {
		log.info("注册会员参数,RegisterMemberDto = {} ", JSON.toJSONString(dto));
		Result result = DubboResult.getDefResult();
		try {
			if (dto != null) {
				Member member = dto.getMeber();
				String[] fields = { "memberPhone", "memberPwd" };
				boolean flag = true;
				if (ParamVerifyUtil.entityIsNotNullByField(member, fields)) {
					TradersPassword tpwd = dto.getTradersPassword();
					if (tpwd != null) {
						String[] pwdfields = { "customerType", "payPwd", "relatedId" };
						flag = ParamVerifyUtil.entityIsNotNullByField(tpwd, pwdfields);
					}
					if (flag) {
						result = memberDbService.registerMember(dto);
					}
				}
			}
		} catch (Exception e) {
			log.error("注册会员信息错误", e);
			result = DubboResult.getResultByError(result);
		}
		return result;
	}

	@Override
	public Result memberAuthentication(MemberInfoDto dto) {
		log.info("会员认证参数,MemberInfoDto = {} ", JSON.toJSONString(dto));
		Result result = DubboResult.getDefResult();
		try {
			String[] fields = { "id", "idCard" };
			if (ParamVerifyUtil.entityIsNotNullByField(dto, fields)) {
				result = memberDbService.memberAuthentication(dto);
			}
		} catch (Exception e) {
			log.error("会员认证错误", e);
			result = DubboResult.getResultByError(result);
		}
		return result;
	}

	@Override
	public Result memberFrozen(MemberInfoDto dto) {
		log.info("会员冻结参数,MemberInfoDto = {} ", JSON.toJSONString(dto));
		Result result = DubboResult.getDefResult();
		try {
			String[] fields = { "id" };
			if (ParamVerifyUtil.entityIsNotNullByField(dto, fields)) {
				result = memberDbService.memberFrozen(dto);
			}
		} catch (Exception e) {
			log.error("会员冻结错误", e);
			result = DubboResult.getResultByError(result);
		}
		return result;
	}

	@Override
	public Result updateMemberPassword(MemberPasswordDto dto) {
		log.info("会员修改密码参数,MemberPasswordDto = {} ", JSON.toJSONString(dto));
		Result result = DubboResult.getDefResult();
		try {
			String[] fields = { "memberPhone", "newPassword"};
			if (ParamVerifyUtil.entityIsNotNullByField(dto, fields)) {
				result = memberDbService.memberUpdatePassword(dto);
			}
		} catch (Exception e) {
			log.error("修改密码错误", e);
			result = DubboResult.getResultByError(result);
		}
		return result;
	}

	@Override
	public Result backMmberPassword(MemberPasswordDto dto) {
		log.info("会员找回密码参数,MemberPasswordDto = {} ", JSON.toJSONString(dto));
		Result result = DubboResult.getDefResult();
		try {
			String[] fields = { "memberPhone", "newPassword" };
			if (ParamVerifyUtil.entityIsNotNullByField(dto, fields)) {
				result = memberDbService.backMemberPassword(dto);
			}
		} catch (Exception e) {
			log.error("找回密码错误", e);
			result = DubboResult.getResultByError(result);
		}
		return result;
	}

	@Override
	public Result memberRelievefrozen(MemberInfoDto dto) {
		log.info("会员解除冻结参数,MemberInfoDto = {} ", JSON.toJSONString(dto));
		Result result = DubboResult.getDefResult();
		try {
			String[] fields = { "id" };
			if (ParamVerifyUtil.entityIsNotNullByField(dto, fields)) {
				result = memberDbService.memberRelievefrozen(dto);
			}
		} catch (Exception e) {
			log.error("会员解除冻结错误", e);
			result = DubboResult.getResultByError(result);
		}
		return result;
	}

	@Override
	public Result memberLogin(MemberLoginDto dto) {
		log.info("会员登录参数,MemberLoginDto = {} ", JSON.toJSONString(dto));
		Result result = DubboResult.getDefResult();
		try {
			String[] fields = { "phone" , "password" };
			if (ParamVerifyUtil.entityIsNotNullByField(dto, fields)) {
				result = memberDbService.memberVerifyLogin(dto);
			}
		} catch (Exception e) {
			log.error("会员登录错误", e);
			result = DubboResult.getResultByError(result);
		}
		return result;
	}

	@Override
	public Result addMemberPayPwd(TradersPassword tradersPassword) {
		log.info("添加会员支付密码参数,TradersPassword = {} ", JSON.toJSONString(tradersPassword));
		Result result = DubboResult.getDefResult();
		try {
			String[] pwdfields = { "customerType", "payPwd" , "relatedId" ,"createUser" };
			if(ParamVerifyUtil.entityIsNotNullByField(tradersPassword, pwdfields)){
				result = memberDbService.addMemberPayPwd(tradersPassword);
			}
		} catch (Exception e) {
			log.error("添加会员支付密码信息错误", e);
			result = DubboResult.getResultByError(result);
		}
		return result;
	}
	/**
	 * 根据手机号获取支付密码
	 */
	@Override
	public Result getMemberPayPwdInfoByMobile(String mobile) {
		log.info("根据手机号获取支付密码参数：" + mobile);
		Result result = DubboResult.getDefResult();
		try {
			if(StringUtils.isNotBlank(mobile)){
				result = memberDbService.queryMemberPayPwdByMobile(mobile);
			}
		} catch (Exception e) {
			log.error("根据手机号获取支付密码信息错误", e);
			result = DubboResult.getResultByError(result);
		}
		return result;
	}

	@Override
	public Result getPayPwdInfo(PayPasswordDto dto) {
		log.info("查询支付密码参数,PayPasswordDto = {} ", JSON.toJSONString(dto));
		Result result = DubboResult.getDefResult();
		try {
			String[] pwdfields = { "relatedId", "customerType" };
			if(ParamVerifyUtil.entityIsNotNullByField(dto, pwdfields)){
				result = memberDbService.queryPayPwdInfo(dto);
			}
		} catch (Exception e) {
			log.error("查询支付密码信息错误", e);
			result = DubboResult.getResultByError(result);
		}
		return result;
	}

	@Override
	public Result updatePayPassword(PayPasswordDto dto) {
		log.info("修改支付密码参数,PayPasswordDto = {} ", JSON.toJSONString(dto));
		Result result = DubboResult.getDefResult();
		try {
			String[] pwdfields = { "relatedId", "customerType", "updateUser", "newPassword" };
			if (ParamVerifyUtil.entityIsNotNullByField(dto, pwdfields)) {
				result = memberDbService.updatePayPassword(dto);
			}
		} catch (Exception e) {
			log.error("修改支付密码信息错误", e);
			result = DubboResult.getResultByError(result);
		}
		return result;
	}

	@Override
	public Result updateMemberPhone(MemberPhoneUpdataVo vo) {
		log.info("修改会员手机号参数,MemberPhoneUpdataVo = {} ", JSON.toJSONString(vo));
		Result result = DubboResult.getDefResult();
		try {
			String[] pwdfields = { "origPhone", "newPhone", "payPwd" };
			if (ParamVerifyUtil.entityIsNotNullByField(vo, pwdfields)) {
				result = memberDbService.updateMemberPhone(vo);
			}
		} catch (Exception e) {
			log.error("修改会员手机号错误", e);
			result = DubboResult.getResultByError(result);
		}
		return result;
	}
	
	@Override
	public Result verifyPhoneIsExists(String phone) {
		log.info("校验手机号是否存在参数 : " + phone);
		Result result = DubboResult.getDefResult();
		try {
			if(StringUtils.isNotBlank(phone)){
				result = memberDbService.verifyPhoneIsExists(phone);
			}
		} catch (Exception e) {
			log.error("校验手机号是否存在错误", e);
			result = DubboResult.getResultByError(result);
		}
		return result;
	}

}
